<?php

class db
{	#Change these variables to your own specifications
	private $hostname = "localhost";
	private $username = "whatsinaname";
	private $password = "powertothepeople";
	
	#DONT TOUCH
	private $error = "DB ERRORS:<br />";
	private $database = "";
	private $connection;

	private function connect()
	{
		$error .= "CONNECT:<br />";
		$this->connection = mysql_connect($this->hostname, $this->username, $this->password);
		  if(!$this->connection) {
		  	$this->error .= "Could not connect: " . mysql_error() . "<br />";
		} else {
			$this->error .= "No connection error <br />";
		}
	}
	private function dbselect($db)
	{
		$this->error .= "DBSELECT:<br />";
		$this->database = $db;
		$dbselect = mysql_select_db($this->database, $this->connection);
		if(!$dbselect) {
			$this->error .= "Could not select DB: " . mysql_error() . "<br />";
		} else {
			$this->error .= "No dbselect error <br />";
		}
	}
	private function close()
	{
		mysql_close($this->connection);
	}
	public function query($db, $sql)
	{
		$this->error .= "QUERY:<br />";
		$this->connect();
		$this->dbselect($db);
		$result = mysql_query($sql);
		if($result) {
			return $result;
			$this->error .= "No query error <br />";
		}else {
			$this->error .= "The Query failed: " . mysql_error() . "<br />";
			return false;
		}
		$this->close();
	}
	public function tblPrint($result)
	{
		echo "<table border='1'><tr>";
		// printing table headers
		for($i=0; $i<mysql_num_fields($result); $i++)
		{
			$field = mysql_fetch_field($result);
			echo "<td>{$field->name}</td>";
		}
		echo "</tr>";
		// printing table rows
		while($row = mysql_fetch_row($result))
		{
			echo "<tr>";

			// $row is array... foreach( .. ) puts every element
			// of $row to $cell variable
			foreach($row as $cell)
				echo "<td>$cell</td>";

				echo "</tr>";
		}
	}
	public function error()
	{
		$this->error .= "---END OF DB ERRORS---";
		return $this->error;
	}
}
